﻿using ERPPublic;
using FinancialManagement.Application.Command.ActualRefundCommand;
using FinancialManagement.Application.Command.SalesReturnCommand;
using FinancialManagement.Domain.Dto;
using FinancialManagement.Dtos;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;

namespace FinancialManagement.Controllers
{
    /// <summary>
    /// 实退销售款
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ActualRefundController : ControllerBase
    {
        protected readonly IMediator mediatorp;
        public ActualRefundController(IMediator mediatorp)
        {
            this.mediatorp = mediatorp;
        }

        /// <summary>
        /// 显示实退销售款
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<PageDto<ListActualRefundDto>>> GetSalesReturn([FromQuery] ListActualRefundCommand command)
        {
            ApiResult<PageDto<ListActualRefundDto>> result = new ApiResult<PageDto<ListActualRefundDto>>();
            try
            {
                result.Data = await mediatorp.Send(command);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 删除实退销售款
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> DelSalesReturn(DelActualRefundCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                var userId = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value; //获取用户ID
                command.Office_Id = sid;
                command.DelName = name;
                command.User_Id = int.Parse(userId);
                result.Data = await mediatorp.Send(command);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }


        /// <summary>
        /// 反填实退销售款
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<ListActualRefundDto>> FanSalesReturn([FromQuery] FanActualRefundCommand command)
        {
            ApiResult<ListActualRefundDto> result = new ApiResult<ListActualRefundDto>();
            try
            {
                result.Data = await mediatorp.Send(command);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 修改实退销售款
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdSalesReturn(UpdActualRefundCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await mediatorp.Send(command);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }





    }
}
